package com.cloud.phone.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.cloud.phone.R;


public class TipsDialog extends Dialog {
    public ViewHolder holder;
    public Context context;
    public TipsDialog(Context context) {
        super(context, R.style.ActionSheetDialogStyle);
        View view = View.inflate(context, R.layout.dialog_tips_normal, null);
        this.context=context;
        setContentView(view);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //设置动画
        dialogWindow.setWindowAnimations(R.style.ActionSheetDialogAnimation);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ScreenUtils.getScreenWidth()-dipTopx(context,64);
        //       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        holder = new ViewHolder(view);
        holder.tvcancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }


    /**
     * dp --> px
     *
     * @param context
     * @param dipValue
     * @return
     */
    public  int dipTopx(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public void showMsg(String msg){
        holder.tvContent.setText(msg);
        holder.tvSure.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                dismiss();
            }
        });
        show();
    }

    public void showMsg(String msg, View.OnClickListener onSureClick){
        holder.tvContent.setText(msg);
        holder.tvSure.setOnClickListener(onSureClick);
        show();
    }

    public void showMsg(String msg,String buttonText, View.OnClickListener onSureClick){
        holder.tvContent.setText(msg);
        holder.tvSure.setOnClickListener(onSureClick);
        holder.tvSure.setText(buttonText);
        show();
    }

    public void showMsgWithCancle(String msg, View.OnClickListener onSureClick,View.OnClickListener onCancleClick){
        holder.tvContent.setText(msg);
        holder.tvSure.setOnClickListener(onSureClick);
        holder.tvcancel.setVisibility(View.VISIBLE);
        holder.tvcancel.setOnClickListener(onCancleClick);
        holder.tvcancel.setText(context.getResources().getString(R.string.cancel));
        holder.tvSure.setText(context.getResources().getString(R.string.sure));
        show();
    }

    public void showMsgWithCancle(String msg,String sureText,String cancelText, View.OnClickListener onSureClick,View.OnClickListener onCancleClick){
        holder.tvContent.setText(msg);
        holder.tvSure.setOnClickListener(onSureClick);
        holder.tvcancel.setOnClickListener(onCancleClick);
        holder.tvcancel.setVisibility(View.VISIBLE);
        holder.tvSure.setText(sureText);
        holder.tvcancel.setText(cancelText);
        holder.tvcancel.setText(context.getResources().getString(R.string.cancel));
        holder.tvSure.setText(context.getResources().getString(R.string.sure));
        show();
    }

    public ViewHolder getHolder() {
        return holder;
    }

    public static class ViewHolder {
        public TextView tvContent;
        public TextView tvcancel;
        public TextView tvSure;

        public  ViewHolder(View view) {
            tvContent = (TextView) view.findViewById(R.id.tv_content);
            tvcancel = (TextView) view.findViewById(R.id.tv_cancel);
            tvSure = (TextView) view.findViewById(R.id.tv_sure);
        }

        public View getView(View view,int id){
            return view.findViewById(id);
        }
    }
}
